package com.ymttest.database.sqlwapper.shenqi;

import java.util.HashMap;
import java.util.List;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.shenqi.SqliveClickRecord;

public class SqliveClickRecordWrapper {

	SqlManager manager;

	public SqliveClickRecordWrapper() {
		manager = new SqlManager("liveorder");
	}

	@SuppressWarnings("unchecked")
	public SqliveClickRecord selectByPrimaryKey(long id) {

		SqliveClickRecord sqliveClickRecord = new SqliveClickRecord();
		List<SqliveClickRecord> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqi.SqliveClickRecordMapper.selectByPrimaryKey",
						id);
		manager.commit();

		if (result.size() > 0) {
			sqliveClickRecord = result.get(0);
		}

		return sqliveClickRecord;
	}
	
	@SuppressWarnings("unchecked")
	public SqliveClickRecord selectByUserIdAndSellerIdAndProductId(Long userId, Long sellerId, String productId) {

		HashMap<String, Object> map = new HashMap<>();
		
		map.put("user_id", userId);
		map.put("seller_id", sellerId);
		map.put("product_id", productId);
		
		SqliveClickRecord sqliveClickRecord = null;
		
		List<SqliveClickRecord> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqi.SqliveClickRecordMapper.selectByUserIdAndSellerIdAndProductId",
						map);
		manager.commit();

		if (result.size() > 0) {
			sqliveClickRecord = result.get(0);
		}

		return sqliveClickRecord;
	}
	
	/**
	 * 查询3小时内的订单
	 * @param userId
	 * @param sellerId
	 * @param productId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public SqliveClickRecord selectIn3Hour(Long userId, Long sellerId, String productId) {

		HashMap<String, Object> map = new HashMap<>();
		
		map.put("user_id", userId);
		map.put("seller_id", sellerId);
		map.put("product_id", productId);
		
		SqliveClickRecord sqliveClickRecord = new SqliveClickRecord();
		List<SqliveClickRecord> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqi.SqliveClickRecordMapper.selectIn3Hour",
						map);
		manager.commit();

		if (result.size() > 0) {
			sqliveClickRecord = result.get(0);
		}

		return sqliveClickRecord;
	}
	
	public void updateAddTimeByUserIdAndSellerIdAndProductId(Long userId, Long sellerId, String productId, String addTime) {

		HashMap<String, Object> map = new HashMap<>();
		
		map.put("user_id", userId);
		map.put("seller_id", sellerId);
		map.put("product_id", productId);
		map.put("add_time", addTime);
		
		manager.update(
				"com.ymttest.database.mapping.shenqi.SqliveClickRecordMapper.updateAddTimeByUserIdAndSellerIdAndProductId",
				map);
		manager.commit();
	}
	
	public static void main(String[] args) {
		
		SqliveClickRecordWrapper wrapper = new SqliveClickRecordWrapper();
		
		wrapper.updateAddTimeByUserIdAndSellerIdAndProductId(4784385L,
				582934823L, "psdf1", "2020-08-21 13:44:10");
	}
}
